(function () {
    'use strict';

    angular
        .module('app.reports')
        .controller('reportsController', reportsController);

    reportsController.$inject = ['logger', '$state', '$rootScope', '$stateParams', 'UserReferralData','DefaultListsProvider','PatientDemoData','$mdDialog'];
    /* @ngInject */
    function reportsController(logger, $state, $rootScope, $stateParams, UserReferralData, DefaultListsProvider, PatientDemoData, $mdDialog) {
        var vm = this;
        vm.ReferralType = 'All';
        vm.ReferralStatus = 'All';
        vm.FromPractice = 'All';
        vm.ToPractice = 'All';
        vm.ReferringPhysician = 'All';
        vm.ReferredPhysician = 'All';
        vm.FromDate = new Date();
        vm.ToDate = new Date();
        vm.maxDate = new Date();
        vm.FilterVisible = true;
        vm.ToggleFilters = ToggleFilters;
        vm.SelectReferral = [];
        vm.FilteredReferrals = [];
        vm.FilterFromPhysician = FilterFromPhysician;
        vm.FilterToPhysician = FilterToPhysician;
        vm.GetFilteredReferrals = GetFilteredReferrals;
        vm.OnReferralSelect = OnReferralSelect;
        vm.GetPracticeByID = GetPracticeByID;
        vm.GetPhysicianByID = GetPhysicianByID;
        vm.FromPhysicianName = 'All';
        vm.ToPhysicianName = 'All';
        vm.FromPracticeName = 'All';
        vm.ToPracticeName = 'All';
        vm.FromDateFormatted = vm.FromDate.format("mm/dd/yy");
        vm.ToDateFormatted = vm.ToDate.format("mm/dd/yy");
        vm.PrintReport = PrintReport;
        vm.onPaginate = onPaginate;


        activate();
        
        function activate() {
            $rootScope.isAuthenticated = true;

            if ($rootScope.isAuthenticated) {
                vm.Physicians = DefaultListsProvider.GetPhysiciansList();
                vm.PracticesList = DefaultListsProvider.GetPracticesList();
            }
            else
            {
                //$state.go('login');
            }
        }

        function ToggleFilters() {
            vm.FilterVisible = !vm.FilterVisible;
        }

        function FilterFromPhysician(Phy) {
            if (vm.FromPractice != 'All')
            {
                return Phy.PracticeID === vm.FromPractice;
            }
            else {
                return true;
            }
        }

        function FilterToPhysician(Phy) {
            if (vm.ToPractice != 'All') {
                return Phy.PracticeID === vm.ToPractice;
            }
            else {
                return true;
            }
        }

        function onPaginate(page, limit) {
            vm.FilteredReferrals = PatientDemoData.GetAdvancedSearchResultsByPage(page - 1, limit);
        }

        function GetFilteredReferrals() {
            var promise = PatientDemoData.GetAdvancedSearchResults(vm.FromDate.format("mm/dd/yy"), vm.ToDate.format("mm/dd/yy"), vm.ReferralType, vm.ReferralStatus, vm.FromPractice, vm.ToPractice, vm.ReferringPhysician, vm.ReferredPhysician)
                        .then(function (response) {
                            if (response.data.ErrorMessage != 'No Data') {
                                vm.SearchCount = PatientDemoData.SetAdvancedSearchResult(response.data.SearchResponse);
                                vm.FilteredReferrals = PatientDemoData.GetAdvancedSearchResultsByPage(0, 10);
                            }
                            else
                            {
                                $mdDialog.show(
                                    $mdDialog.alert()
                                    .parent(angular.element(document.body))
                                    .clickOutsideToClose(true)
                                    .title('Search Result')
                                    .textContent('No records found!')
                                    .ariaLabel('Search ResultDialog')
                                    .ok('Close')
                                );
                                vm.SearchCount = 0;
                                vm.FilteredReferrals = [];
                            }
                            });
        }
        
        function OnReferralSelect() {
            $state.go('category', { Type: 'Referral', Category: vm.SelectReferral });
            //alert('Selected' + vm.SelectReferral);
        }

        function GetPracticeByID(value,source)
        {
            var match = 0;
            var text = 'All';
            for (var i = 0; i < vm.PracticesList.length; i++) {
                if(vm.PracticesList[i].PracticeID == value)
                {
                    match++;
                    text = vm.PracticesList[i].PracticeName;
                    break;
                }
            }
                
            if (source == 'from') {
                vm.FromPracticeName = text;
            }
            else if (source == 'to') {
                vm.ToPracticeName = text;
            }
        }

        function GetPhysicianByID(value, source) {
            var match = 0;
            var text = 'All';
            for (var i = 0; i < vm.Physicians.length; i++) {
                if (vm.Physicians[i].PhysicianID == value) {
                    match++;
                    text = vm.Physicians[i].PhysicianName;
                    break;
                }
            }

            if (source == 'from') {
                vm.FromPhysicianName = text;
            }
            else if (source == 'to') {
                vm.ToPhysicianName = text;
            }
        }

        function PrintReport()
        {
            var printContents = document.getElementById('PrintDiv').innerHTML;
            var popupWin = window.open('', '_blank', 'width=300,height=300');
            popupWin.document.open();
            popupWin.document.write('<html><head><link rel="stylesheet" type="text/css" href="style.css" /></head><body onload="window.print()">' + printContents + '</body></html>');
            popupWin.document.close();
        }

    }
})();


